<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/13 0013
 * Time: 17:26
 */

namespace app\admin\controller;
use think\Db;

class Qqlp  extends Bases
{
   public function index(){
       $is_china = intval(input('param.is_china'));
       $where['is_del']=0;
       $where['sign']="pc";
       if($is_china){
           $where['is_china']=$is_china;
           $this->assign('is_china',$is_china);
       }

       $data = Db::name('qqlp')->where($where)->order('sort desc,id desc')->select();

       $this->assign('data',$data);
       return $this->fetch();
   }

   public function add_qqlp(){
       $is_china = intval(input('param.is_china'));


       $this->assign('is_china',$is_china);

       return $this->fetch();
   }
    public function edit_qqlp(){
        $id = input('param.id');
        if($id){
            $data =Db::name('qqlp')->where('id',$id)->find();
            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();
            $data['content'] = html_entity_decode($data['content']);
            $this->assign('imgurl',$works_pic);
            $this->assign('data',$data);

        }
        $is_china = intval(input('param.is_china'));
        echo $is_china;
        $this->assign('is_china',$is_china);
        return $this->fetch();
    }

   public function addqqlp(){

       $id = input('post.id');
       $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';
       $data=[
           'title'=>input('post.title'),
           'intro'=>input('post.intro'),
           'place'=>input('post.place'),
           'is_show'=>intval(input('post.is_show')),
           'is_china'=>intval(input('post.is_china')),
           'sort'=>intval(input('post.sort')),
          'works_photo'=>$array_works_pic,
           'content'=>input('post.content'),
           'click'=>intval(input('post.click')),
           'seo_title'=>input('param.seo_title'),
           'seo_keywords'=>input('param.seo_keywords'),
           'seo_description'=>input('param.seo_description'),
           'sign'=>"pc"
       ];
       $file_main_img = request()->file('main_img');
       if($file_main_img){
           $file_info_main_img = $file_main_img->move('./uploads/qqlp/');
           if($file_info_main_img){
               $data['main_img'] = '/uploads/qqlp/'.$file_info_main_img->getSaveName();
           }else{
               return json(['code'=>0,'msg'=>$file_main_img->getError()]);
           }
       }
       if($id){
           if($file_main_img){
               $old_img = Db::name('qqlp')->where('id',$id)->value('main_img');
               if($old_img){
                   if($old_img !== ' '){
                       unlink('.'.str_replace('\\','/',$old_img));
                   }
               }
           }
           $data['update_time']=time();
           $result = DB::name('qqlp')->where('id',$id)->update($data);
       }else{
           $data['create_time']=time();
           $result = Db::name('qqlp')->insert($data);
       }
       if($result !==false){
           return json(['code'=>1,'msg'=>'操作成功']);
       }else{
           return json(['code'=>0,'msg'=>'操作失败']);
       }
   }

    //删除
    public function qqlpdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('qqlp')->where('id',$id)->update(['is_del'=>1]);;
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    //改变排序
    public function ChangeSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('qqlp')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }
}